﻿package org.xhs.report.common.ui.toolbar
{
    import baidu.lib.utils.*;
    import baidu.ui.controls.*;
    import baidu.ui.managers.*;
    import baidu.wenku.common.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.common.ui.panel.*;
    import baidu.wenku.common.utils.*;
    import baidu.wenku.txt.ui.panel.*;
    import flash.display.*;
    import flash.events.*;
    import flash.external.*;

    public class ToolButtonLayout extends AutoLayoutContainer
    {
        protected var logo:Sprite;
        protected var newmarkBtn:Button;
        protected var zoominBtn:Button;
        protected var zoomoutBtn:Button;
        protected var singleBtn:Button;
        protected var doubleBtn:Button;
        protected var zooms:Button;
        protected var zoomm:Button;
        protected var zooml:Button;
        protected var bgColorBtn:Button;
        protected var fullBtn:Button;
        protected var normalBtn:Button;
        protected var seriesBtn:Button;
        protected var seriesNewIcon:MovieClip;
        protected var downloadBtn:Button;
        protected var shareBtn:Button;
        protected var relativeBtn:Button;
        protected var splitor1:Sprite;
        protected var splitor2:Sprite;
        protected var skins:MovieClip;
        protected var buttonInfo:LayoutInfo;
        protected var seriesModel:SeriesModel;
        protected var screenModel:ScreenModel;
        protected var loaderInfoVo:LoaderInfoVO;
        protected var sharePanel:SharePanel;
        protected var relativeModel:RelativeModel;
        protected var relativePanel:RelativePanel;
        protected var bgColorPanel:BgColorPanel;
        protected const TIPS:Object;

        public function ToolButtonLayout()
        {
            this.TIPS = {zoomin:"放大", zoomout:"缩小", single:"单页浏览", double:"双页浏览", zooms:"小字体", zoomm:"中字体", zooml:"大字体", bgColor:"背景色调节", full:"全屏显示", normal:"恢复窗口", newmark:"书签", series:"系列文档", download:"下载文档", share:"分享文档", relative:"相关文档"};
            this.initModel();
            this.initButtons();
            return;
        }// end function

        protected function initModel() : void
        {
            this.loaderInfoVo = InstanceManager.createSingletonInstance(LoaderInfoVO);
            this.seriesModel = InstanceManager.createSingletonInstance(SeriesModel);
            this.seriesModel.addEventListener(SeriesModel.DATA_SUCCEED, this.seriesDataSucceed);
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.screenModel.addEventListener(Event.RESIZE, this.screenResize);
            this.sharePanel = InstanceManager.createSingletonInstance(SharePanel);
            this.sharePanel.addEventListener(SharePanel.CLOSE_CLICK, this.shareCloseClick);
            this.relativeModel = InstanceManager.createSingletonInstance(RelativeModel);
            this.relativePanel = InstanceManager.createSingletonInstance(RelativePanel);
            this.relativePanel.addEventListener(RelativePanel.CLOSE_CLICK, this.relativeCloseClick);
            this.bgColorPanel = InstanceManager.createSingletonInstance(BgColorPanel);
            this.bgColorPanel.addEventListener(BgColorPanel.BG_COLOR_CHANGED, this.bgColorChanged);
            return;
        }// end function

        protected function initButtons() : void
        {
            this.skins = new AllSkins();
            this.initMisc();
            this.initDocZoom();
            this.initDocPageMode();
            this.initTxtSize();
            this.initTxtBgColor();
            this.initFullscreen();
            this.initAPI();
            this.initSplitor();
            this.screenResize();
            return;
        }// end function

        protected function initMisc() : void
        {
            this.logo = new Logo() as Sprite;
            addChild(this.logo);
            this.buttonInfo = _childsLayoutInfo[this.logo];
            this.buttonInfo.top = Config.TB_HEIGHT / 2;
            this.buttonInfo.left = 12 + this.logo.width / 2;
            if (!Config.isAPIViewer)
            {
                this.logo.visible = false;
            }
            else
            {
                this.logo.buttonMode = true;
                this.logo.addEventListener(MouseEvent.CLICK, function (event:MouseEvent) : void
            {
                BrowserUtil.openNewWindow("http://wenku.baidu.com");
                return;
            }// end function
            );
            }
            this.newmarkBtn = new Button();
            this.newmarkBtn.setStyle("skin", this.skins["skin_newmark"]);
            this.newmarkBtn.drawNow();
            addChild(this.newmarkBtn);
            this.newmarkBtn.useHandCursor = true;
            this.newmarkBtn.addEventListener(MouseEvent.CLICK, this.newmarkBtnClick);
            TooltipManager.getInstance().register(this.newmarkBtn, this.TIPS["newmark"]);
            this.newmarkBtn.enabled = false;
            this.newmarkBtn.visible = Config.isAPIViewer ? (false) : (true);
            this.buttonInfo = _childsLayoutInfo[this.newmarkBtn];
            this.buttonInfo.left = 18;
            this.seriesBtn = new Button();
            this.seriesBtn.visible = false;
            this.seriesBtn.setStyle("skin", this.skins["skin_series"]);
            this.seriesBtn.drawNow();
            addChild(this.seriesBtn);
            this.seriesBtn.useHandCursor = true;
            this.seriesBtn.addEventListener(MouseEvent.CLICK, this.seriesBtnClick);
            TooltipManager.getInstance().register(this.seriesBtn, this.TIPS["series"]);
            this.seriesBtn.enabled = false;
            this.buttonInfo = _childsLayoutInfo[this.seriesBtn];
            this.buttonInfo.left = 20;
            this.buttonInfo.bottom = -this.seriesBtn.height + Config.SB_HEIGHT / 2;
            this.seriesNewIcon = new IconNew() as MovieClip;
            this.seriesNewIcon.visible = false;
            addChild(this.seriesNewIcon);
            this.buttonInfo = _childsLayoutInfo[this.seriesNewIcon];
            this.buttonInfo.left = 83;
            this.buttonInfo.bottom = 14;
            return;
        }// end function

        protected function initDocZoom() : void
        {
            this.zoominBtn = new Button();
            this.zoominBtn.setStyle("skin", this.skins["skin_zoomin"]);
            this.zoominBtn.drawNow();
            this.zoominBtn.useHandCursor = true;
            this.zoominBtn.visible = !Config.isTxt;
            addChild(this.zoominBtn);
            this.zoominBtn.addEventListener(MouseEvent.CLICK, this.zoominBtnClick);
            TooltipManager.getInstance().register(this.zoominBtn, this.TIPS["zoomin"]);
            this.zoominBtn.enabled = false;
            this.buttonInfo = _childsLayoutInfo[this.zoominBtn];
            this.buttonInfo.right = 180 - this.zoominBtn.width;
            this.buttonInfo.top = Config.TB_HEIGHT / 2;
            this.zoomoutBtn = new Button();
            this.zoomoutBtn.setStyle("skin", this.skins["skin_zoomout"]);
            this.zoomoutBtn.drawNow();
            this.zoomoutBtn.useHandCursor = true;
            this.zoomoutBtn.visible = !Config.isTxt;
            addChild(this.zoomoutBtn);
            this.zoomoutBtn.addEventListener(MouseEvent.CLICK, this.zoomoutBtnClick);
            TooltipManager.getInstance().register(this.zoomoutBtn, this.TIPS["zoomout"]);
            this.zoomoutBtn.enabled = false;
            this.buttonInfo = _childsLayoutInfo[this.zoomoutBtn];
            this.buttonInfo.right = 150 - this.zoomoutBtn.width;
            this.buttonInfo.top = Config.TB_HEIGHT / 2;
            return;
        }// end function

        protected function initDocPageMode() : void
        {
            this.singleBtn = new Button();
            this.singleBtn.setStyle("skin", this.skins["skin_single"]);
            this.singleBtn.drawNow();
            this.singleBtn.useHandCursor = true;
            this.singleBtn.visible = !Config.isTxt;
            addChild(this.singleBtn);
            this.singleBtn.addEventListener(MouseEvent.CLICK, this.singleBtnClick);
            TooltipManager.getInstance().register(this.singleBtn, this.TIPS["single"]);
            this.singleBtn.enabled = false;
            this.buttonInfo = _childsLayoutInfo[this.singleBtn];
            this.buttonInfo.right = 105 - this.singleBtn.width;
            this.buttonInfo.top = Config.TB_HEIGHT / 2;
            this.doubleBtn = new Button();
            this.doubleBtn.setStyle("skin", this.skins["skin_double"]);
            this.doubleBtn.drawNow();
            this.doubleBtn.useHandCursor = true;
            this.doubleBtn.visible = !Config.isTxt;
            addChild(this.doubleBtn);
            this.doubleBtn.addEventListener(MouseEvent.CLICK, this.doubleBtnClick);
            TooltipManager.getInstance().register(this.doubleBtn, this.TIPS["double"]);
            this.doubleBtn.enabled = false;
            this.buttonInfo = _childsLayoutInfo[this.doubleBtn];
            this.buttonInfo.right = 75 - this.doubleBtn.width;
            this.buttonInfo.top = Config.TB_HEIGHT / 2;
            return;
        }// end function

        protected function initTxtSize() : void
        {
            this.zooms = new Button();
            this.zooms.setStyle("skin", this.skins["skin_zooms"]);
            this.zooms.drawNow();
            this.zooms.useHandCursor = true;
            this.zooms.visible = Config.docType == Config.DOC_TYPE_TXT;
            addChild(this.zooms);
            this.zooms.addEventListener(MouseEvent.CLICK, this.zoomsBtnClick);
            TooltipManager.getInstance().register(this.zooms, this.TIPS["zooms"]);
            this.zooms.enabled = false;
            this.buttonInfo = _childsLayoutInfo[this.zooms];
            this.buttonInfo.right = 190 - this.zooms.width;
            this.buttonInfo.top = Config.TB_HEIGHT / 2;
            this.zoomm = new Button();
            this.zoomm.setStyle("skin", this.skins["skin_zoomm"]);
            this.zoomm.drawNow();
            this.zoomm.y = Config.TB_HEIGHT / 2;
            this.zoomm.useHandCursor = true;
            this.zoomm.visible = Config.docType == Config.DOC_TYPE_TXT;
            addChild(this.zoomm);
            this.zoomm.addEventListener(MouseEvent.CLICK, this.zoommBtnClick);
            TooltipManager.getInstance().register(this.zoomm, this.TIPS["zoomm"]);
            this.zoomm.enabled = false;
            this.buttonInfo = _childsLayoutInfo[this.zoomm];
            this.buttonInfo.right = 160 - this.zoomm.width;
            this.buttonInfo.top = Config.TB_HEIGHT / 2;
            this.zooml = new Button();
            this.zooml.setStyle("skin", this.skins["skin_zooml"]);
            this.zooml.drawNow();
            this.zooml.y = Config.TB_HEIGHT / 2;
            this.zooml.useHandCursor = true;
            this.zooml.visible = Config.docType == Config.DOC_TYPE_TXT;
            addChild(this.zooml);
            this.zooml.addEventListener(MouseEvent.CLICK, this.zoomlBtnClick);
            TooltipManager.getInstance().register(this.zooml, this.TIPS["zooml"]);
            this.zooml.enabled = false;
            this.buttonInfo = _childsLayoutInfo[this.zooml];
            this.buttonInfo.right = 130 - this.zooml.width;
            this.buttonInfo.top = Config.TB_HEIGHT / 2;
            return;
        }// end function

        protected function initTxtBgColor() : void
        {
            this.bgColorBtn = new Button();
            this.bgColorBtn.setStyle("skin", this.skins["bg_color"]);
            this.bgColorBtn.drawNow();
            this.bgColorBtn.useHandCursor = true;
            this.bgColorBtn.visible = Config.docType == Config.DOC_TYPE_TXT;
            addChild(this.bgColorBtn);
            this.bgColorBtn.addEventListener(MouseEvent.CLICK, this.bgColorBtnClick);
            TooltipManager.getInstance().register(this.bgColorBtn, this.TIPS["bgColor"]);
            this.bgColorBtn.enabled = false;
            this.buttonInfo = _childsLayoutInfo[this.bgColorBtn];
            this.buttonInfo.right = 89 - this.bgColorBtn.width;
            this.buttonInfo.top = Config.TB_HEIGHT / 2;
            return;
        }// end function

        protected function initFullscreen() : void
        {
            this.fullBtn = new Button();
            this.fullBtn.setStyle("skin", this.skins["skin_full"]);
            this.fullBtn.drawNow();
            this.fullBtn.useHandCursor = true;
            addChild(this.fullBtn);
            this.fullBtn.addEventListener(MouseEvent.CLICK, this.fullBtnClick);
            TooltipManager.getInstance().register(this.fullBtn, this.TIPS["full"]);
            this.fullBtn.enabled = false;
            this.buttonInfo = _childsLayoutInfo[this.fullBtn];
            this.buttonInfo.right = 30 - this.fullBtn.width;
            this.buttonInfo.top = Config.TB_HEIGHT / 2;
            this.normalBtn = new Button();
            this.normalBtn.setStyle("skin", this.skins["skin_normal"]);
            this.normalBtn.drawNow();
            this.normalBtn.useHandCursor = true;
            addChild(this.normalBtn);
            this.normalBtn.visible = false;
            this.normalBtn.addEventListener(MouseEvent.CLICK, this.normalBtnClick);
            TooltipManager.getInstance().register(this.normalBtn, this.TIPS["normal"]);
            this.normalBtn.enabled = false;
            this.buttonInfo = _childsLayoutInfo[this.normalBtn];
            this.buttonInfo.right = 30 - this.normalBtn.width;
            this.buttonInfo.top = Config.TB_HEIGHT / 2;
            return;
        }// end function

        protected function initAPI() : void
        {
            this.downloadBtn = new Button();
            this.downloadBtn.setStyle("skin", this.skins["skin_download"]);
            this.downloadBtn.drawNow();
            this.downloadBtn.useHandCursor = true;
            addChild(this.downloadBtn);
            this.downloadBtn.addEventListener(MouseEvent.CLICK, this.downloadBtnClick);
            TooltipManager.getInstance().register(this.downloadBtn, this.TIPS["download"]);
            this.downloadBtn.enabled = false;
            this.buttonInfo = _childsLayoutInfo[this.downloadBtn];
            this.buttonInfo.right = 100 - this.downloadBtn.width;
            this.buttonInfo.bottom = -this.downloadBtn.height + Config.SB_HEIGHT / 2;
            this.downloadBtn.visible = Config.isAPIViewer;
            this.shareBtn = new Button();
            this.shareBtn.setStyle("skin", this.skins["skin_share"]);
            this.shareBtn.drawNow();
            this.shareBtn.useHandCursor = true;
            addChild(this.shareBtn);
            this.shareBtn.addEventListener(MouseEvent.CLICK, this.shareBtnClick);
            TooltipManager.getInstance().register(this.shareBtn, this.TIPS["share"]);
            this.shareBtn.enabled = false;
            this.buttonInfo = _childsLayoutInfo[this.shareBtn];
            this.buttonInfo.right = 70 - this.shareBtn.width;
            this.buttonInfo.bottom = -this.shareBtn.height + Config.SB_HEIGHT / 2;
            this.shareBtn.visible = Config.isAPIViewer;
            this.relativeBtn = new Button();
            this.relativeBtn.setStyle("skin", this.skins["skin_relative"]);
            this.relativeBtn.drawNow();
            this.relativeBtn.useHandCursor = true;
            addChild(this.relativeBtn);
            this.relativeBtn.addEventListener(MouseEvent.CLICK, this.relativeBtnClick);
            TooltipManager.getInstance().register(this.relativeBtn, this.TIPS["relative"]);
            this.relativeBtn.enabled = false;
            this.buttonInfo = _childsLayoutInfo[this.relativeBtn];
            this.buttonInfo.right = 40 - this.relativeBtn.width;
            this.buttonInfo.bottom = -this.relativeBtn.height + Config.SB_HEIGHT / 2;
            this.relativeBtn.visible = Config.isAPIViewer;
            return;
        }// end function

        protected function initSplitor() : void
        {
            this.splitor1 = new Splitor();
            this.splitor1.y = Config.TB_HEIGHT / 2;
            addChild(this.splitor1);
            this.buttonInfo = _childsLayoutInfo[this.splitor1];
            this.buttonInfo.right = 55 - this.splitor1.width;
            this.buttonInfo.top = Config.TB_HEIGHT / 2;
            this.splitor2 = new Splitor();
            this.splitor2.y = Config.TB_HEIGHT / 2;
            addChild(this.splitor2);
            this.buttonInfo = _childsLayoutInfo[this.splitor2];
            this.buttonInfo.right = (Config.isTxt ? (110) : (130)) - this.splitor2.width;
            this.buttonInfo.top = Config.TB_HEIGHT / 2;
            return;
        }// end function

        protected function screenResize(event:Event = null) : void
        {
            if (this.screenModel.stageWidth > 0 && this.screenModel.stageHeight > 0)
            {
                width = this.screenModel.stageWidth;
                height = this.screenModel.stageHeight;
                this.logo.visible = width > 440 && Config.isAPIViewer;
                this.fullBtn.visible = !this.screenModel.isFullscreen;
                this.normalBtn.visible = this.screenModel.isFullscreen;
            }
            return;
        }// end function

        protected function newmarkBtnClick(event:MouseEvent) : void
        {
            WenkuLog.addLog(WenkuLog.LOGURL_NEWMARK_BTN);
            ExternalInterface.call(JSInterface.AS_BOOKMARK);
            return;
        }// end function

        protected function seriesDataSucceed(event:Event) : void
        {
            if (this.seriesModel.hasData && this.seriesModel.seriesLength)
            {
                this.seriesBtn.enabled = true;
                this.seriesBtn.visible = true;
                this.seriesNewIcon.visible = true;
                WenkuLog.addLog(WenkuLog.LOGURL_SERIES_EXIST);
            }
            return;
        }// end function

        protected function seriesBtnClick(event:MouseEvent) : void
        {
            WenkuLog.addLog(WenkuLog.LOGURL_SERIES_BTN);
            this.seriesModel.isOpen = true;
            event.stopPropagation();
            return;
        }// end function

        protected function zoominBtnClick(event:MouseEvent = null) : void
        {
            dispatchEvent(new ToolButtonEvent(ToolButtonEvent.ZOOM_IN));
            return;
        }// end function

        protected function zoomoutBtnClick(event:MouseEvent = null) : void
        {
            dispatchEvent(new ToolButtonEvent(ToolButtonEvent.ZOOM_OUT));
            return;
        }// end function

        protected function singleBtnClick(event:MouseEvent = null) : void
        {
            dispatchEvent(new ToolButtonEvent(ToolButtonEvent.MODE_SINGLE));
            return;
        }// end function

        protected function doubleBtnClick(event:MouseEvent = null) : void
        {
            dispatchEvent(new ToolButtonEvent(ToolButtonEvent.MODE_DOUBLE));
            return;
        }// end function

        protected function zoomsBtnClick(event:MouseEvent = null) : void
        {
            dispatchEvent(new ToolButtonEvent(ToolButtonEvent.SIZE_SMALL));
            return;
        }// end function

        protected function zoommBtnClick(event:MouseEvent = null) : void
        {
            dispatchEvent(new ToolButtonEvent(ToolButtonEvent.SIZE_MODERATE));
            return;
        }// end function

        protected function zoomlBtnClick(event:MouseEvent = null) : void
        {
            dispatchEvent(new ToolButtonEvent(ToolButtonEvent.SIZE_LARGE));
            return;
        }// end function

        protected function bgColorBtnClick(event:MouseEvent) : void
        {
            event.stopPropagation();
            this.bgColorPanel.isOpen = true;
            return;
        }// end function

        protected function bgColorChanged(event:Event) : void
        {
            dispatchEvent(new ToolButtonEvent(ToolButtonEvent.BG_COLOR));
            return;
        }// end function

        protected function fullBtnClick(event:MouseEvent = null) : void
        {
            dispatchEvent(new ToolButtonEvent(ToolButtonEvent.SCREEN_FULL));
            return;
        }// end function

        protected function normalBtnClick(event:MouseEvent = null) : void
        {
            dispatchEvent(new ToolButtonEvent(ToolButtonEvent.SCREEN_NORMAL));
            return;
        }// end function

        protected function downloadBtnClick(event:MouseEvent) : void
        {
            WenkuLog.addLog(WenkuLog.LOGURL_DOWNLOAD, "", 2);
            var _loc_2:* = this.loaderInfoVo.domainUrl + "view/" + this.loaderInfoVo.docId + ".html#flag";
            BrowserUtil.openNewWindow(_loc_2);
            return;
        }// end function

        protected function shareBtnClick(event:MouseEvent) : void
        {
            WenkuLog.addLog(WenkuLog.LOGURL_SHAREBUTTON, "", 2);
            this.addSharePanel();
            return;
        }// end function

        protected function addSharePanel() : void
        {
            if (this.sharePanel)
            {
                this.sharePanel.resetPanel();
            }
            return;
        }// end function

        protected function shareCloseClick(event:Event) : void
        {
            if (this.sharePanel)
            {
                this.sharePanel.destroy();
            }
            return;
        }// end function

        protected function relativeBtnClick(event:MouseEvent) : void
        {
            WenkuLog.addLog(WenkuLog.LOGURL_RELATIVE, "", 2);
            this.relativeModel.isOpen = true;
            return;
        }// end function

        protected function relativeCloseClick(event:Event) : void
        {
            this.relativeModel.isOpen = false;
            return;
        }// end function

        public function disableBtnByName(param1:String) : void
        {
            var _loc_2:Button = null;
            try
            {
                _loc_2 = this[param1] as Button;
                if (_loc_2)
                {
                    _loc_2.enabled = false;
                }
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        public function enableBtnByName(param1:String) : void
        {
            var _loc_2:Button = null;
            try
            {
                _loc_2 = this[param1] as Button;
                if (_loc_2)
                {
                    _loc_2.enabled = true;
                }
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

    }
}
